<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>组织机构管理</title>
<block th:replace="commonBase"></block>   
</head>
<body>
    <div class="mini-splitter" handlerSize="6"  style="width:100%;height:100%;">
      <!-- 左部查询树开始 -->
	  <div size="20%" showCollapseButton="false">
	    <div class="mini-toolbar" style="padding:2px;border-top:0;border-left:0;border-right:0;">
			<span style="padding-left:5px;">名称：</span> 
			<input id="key" emptyText="请输入机构名称"
				class="mini-textbox" style="width:120px;" onenter="orgDimTreeSearch" AUTOCOMPLETE="on" /> 
			<a class="mini-button" iconCls="icon-search" plain="true" onclick="orgDimTreeSearch()">查找</a>  
		</div>
		<div class="mini-fit">
			<ul id="tree1" class="mini-tree" style="width:100%;"
				showTreeIcon="true" textField="orgName" idField="id" expandOnLoad="0"  onnodeclick="csearch()"
				parentField="parentId" resultAsTree="false"  allowDrag="true" allowDrop="true">
			</ul>
		</div> 
     </div>
     <!-- 左部查询树结束 -->
    
     <!-- 右部查询列表开始 -->
     <div showCollapseButton="false" >
        <!-- 菜单栏开始 -->
	 	<div style="width:100%;">
			<div class="mini-toolbar" style="border-bottom:0;padding:0px;">
				<table style="width:100%;">
					<tr>
		                 <td style="width:100%;" id="operationId"> 
		                 	<a sec:authorize="hasAuthority('org:add')"   class="mini-button"  plain="true"   iconCls="icon-addnew" onclick="add">添加</a>
		                 	<a sec:authorize="hasAuthority('org:update')"   class="mini-button"  plain="true"   iconCls="icon-edit" onclick="edit">编辑</a>
		                 	<a sec:authorize="hasAuthority('org:delete')"   class="mini-button"  plain="true"  iconCls="icon-remove" onclick="remove">删除</a> 
		                 </td>	
						 <td style="white-space:nowrap;">
							<input id="searchKey" class="mini-textbox" emptyText="请输入名称或编码" style="width:150px;" onenter="search"/>
							<a class="mini-button" onclick="search()" iconCls="icon-search">查询</a>
						 </td> 
					</tr>
				</table>
			</div>
		</div>
		<!-- 菜单栏结束 -->
		<!-- 列表展示开始 -->
		<div class="mini-fit">
			<div id="grid1" class="mini-datagrid" style="width:100%;height:100%;" 
				allowCellSelect="true" multiSelect="true" allowAlternating="false" sortField="org_level,org_Code,org_Name" sortOrder="desc" idField="id" pageSize="20" sizeList="[10,20,50,100]"
				 th:attr="url=@{/org/pagelist}"  >
				<div property="columns">
					<div type="checkcolumn"></div>
					<div field="orgName"  sortField="ORG_NAME" width="100" allowSort="true" headerAlign="center" align="left">组织机构名称</div>
					<div field="orgCode" sortField="ORG_CODE" width="100" allowSort="true" headerAlign="center" align="right">组织机构编号</div>
					<div field="orgType" width="100" renderer="onOpenOrgType" headerAlign="center" align="left">组织属性</div>
					
					<div field="sevenId" width="100" renderer="onOpenOrgSeven" headerAlign="center" align="left">所属七区域</div>
					
					<div field="parentId" width="100" renderer="onOpenOrgName" headerAlign="center" align="left">所属上级机构</div>
					<div field="linkMan" width="100" headerAlign="center" align="left">联系人</div>   
					<div field="linkPhone" width="100" headerAlign="center" align="left">联系电话</div>
					<div field="zipCode" width="100" headerAlign="center" align="right">邮编</div>
					<div field="orgAddress" width="100" headerAlign="center" align="left">地址</div>  
					<div field="isEnable" width="100"  renderer="onOpenURenderer" headerAlign="center" align="center">是否启用</div>
					<div field="remark" width="100" headerAlign="center" align="left">备注</div>
				</div>
			</div>
		 </div>
		 <!-- 列表展示结束 -->
	  </div>
   </div>		  
</body>
<script th:inline="javascript" type="text/javascript">
 /*<![CDATA[*/
	var orgNameList = null;
	var grid =  null;//表格对象
	var tree = null;//树对象
	$(function(){
		  mini.parse();
		  grid = mini.get("grid1");
		  tree = mini.get("tree1");
		  queryOrg();//缓存所有组织机构
		  	//加载完成之后选择第一条
		   grid.on("load", function (e) {
		   		var rows = grid.getRow(0);
		   		grid.clearSelect(false);
		    	grid.setSelected(rows);
		   });
		  
	});
	
	function add(){
		 mini.open({
	        url:basePath+"org/edit",
	        title: "新增机构", iconCls:'icon-addnew', width: 528, height: 460,
	        onload: function () {
	            var iframe = this.getIFrameEl();
	            var selectNode = tree.getSelectedNode();//获取选中的节点
	            
	             var parentId  =  "1";
	           	 var parentIds = "0,1";
	           	 var parentName = "全国";
	           	 var orgLevel = "1";
	            
	            if(!!selectNode){//如果选中了节点
	            	var pnode  = tree.getParentNode(selectNode);
	            	parentId = selectNode.id;//获取当前的parentId
	            	var parentNodes = tree.getAncestors(selectNode);
	                parentName= getAllOrgName(parentNodes,false)+selectNode.orgName;//获取所有组织机构名称
	                
	                parentIds = getAllOrgId(parentNodes,false)+selectNode.id;//获取组织机构ID
	            	orgLevel = tree.getLevel(selectNode)+1;//当前级别 
	            }
	            
	            //var parentId = tree.getList();
	            var data = { action:"new",parentId:parentId,parentIds:parentIds,parentName:parentName,orgLevel:orgLevel};
	            iframe.contentWindow.setData(data);
	        },
	        ondestroy: function (action) {
				if (action != "close" && action != "cancle"){
	        		//grid.reload();
	        		queryOrg(action);
				}
	        }
	    });
	
	}
	
	
	function edit(){
		var rows = grid.getSelecteds();
		if(rows.length>1){
			mini.alert("只能选择一条记录!");
			return;
		}
		var row = grid.getSelected();
	    if (row) {
				 mini.open({
			        url:basePath+"org/edit",
			        title: "编辑机构", iconCls:'icon-edit', width: 528, height: 460,
			        onload: function () {
			             var iframe = this.getIFrameEl();
			             var selectNode = tree.getNode(row.id);//获取选中的节点
			             
			             
			             
			             var parentNodes = tree.getAncestors(selectNode);
			             
			             var parentName= getAllOrgName(parentNodes,true);//获取所有组织机构名称
			             
			             var parentIds = getAllOrgId(parentNodes,true);//获取组织机构ID
			             
			            // alert(parentName);
			             
			             var orgLevel = tree.getLevel(selectNode);//当前级别 
			            
			            //var parentId = tree.getList();
			            var data = { action:"edit",parentIds:parentIds,parentName:parentName,fwRightOrg:row,orgLevel:orgLevel};
			            iframe.contentWindow.setData(data);
			        },
			        ondestroy: function (action) {
						if (action != "close" && action != "cancle"){
			        		//grid.reload();
			        		queryOrg(action);
						}
			        }
			    });
	    }
	
	}
	
		// 机构删除
	function remove(){
		var rows = grid.getSelecteds();
		if (rows.length > 0 ){
			//mini.confirm("确定删除选中记录？","提示!",function(e){
				//if (e == "ok"){
					var rowArray = [];// 当前删除行的数组
					var rowIndex = 0;
				    if (rows.length > 0) {
				    	var isLeaf = true;//判断选择的是否全部都是叶子节点，非叶子节点不能删除
				        // 当前选中列数据循环检测
				  		$.each(rows,function (idx,rowsObj){
				  			var nid = rowsObj.id;
				  			var node = tree.getNode(nid);
				  			if(!tree.isLeaf(node)){
				  			    showMsg("只能删除末节点！","danger",2000,"center","center");
				  				return false;
				  			}
							// 删除行数组赋值
							//rowArray[rowIndex++] = "'"+nid+"'";
							rowArray[rowIndex++] = nid;
				  		});
				       if (rowArray.length != 0){
				       		var ids = rowArray.toString();
				       		 $.ajax({
				       			url:basePath+"org/delete?ids="+ids,
				       			type:"delete",
				       			data:null,
				       			success:function(successData){
				       	        	var message = mini.decode(successData);
						        	if(message.type == '200'){
						            	showMsg("机构删除成功","success",800,"center","center");
						            		//下次定位过来
						            		var orgNode = tree.getSelectedNode();
						            		var parentId = null;
						            		if(!!orgNode){
						            			parentId = orgNode.id;//获取parentId
						            			//如果选中的是叶子节点
						            			if(tree.isLeaf(orgNode)){
						            				parentId = orgNode.parentId;
						            			}
						            		}
											
						        			queryOrg(parentId);
						        	}else{
						        		showMsg("机构删除失败","warning",800,"center","center");
						        	}
				       			},
				       			error:function(errorData){
		        	 				showMsg("机构删除错误","danger",800,"center","center");
				       			}
				       		}); 
				       }
				    }
			//	}
			//});
		}else{
			mini.alert("请选中一条记录!");
		}
	}
	
	//缓存所有组织机构
	function queryOrg(orgId){
		 $.ajax({
			 url:basePath+"org/list",
			 type: 'POST',
		     async:false,
			 success : function(text) {
				 orgNameList = mini.decode(text);
				// tree.loadData(orgNameList);//load数据
				 tree.loadList(orgNameList, "id", "parentId");
				 //如果存在orgId
				 if(!!orgId){
			        tree.selectNode(orgId);
			        var tnode  =tree.getSelectedNode();
			        if(tree.isLeaf(tnode)){
			        	tree.expandPath(orgId);
			        }else{
			            if (!!tnode) {
			            	tree.expandPath(orgId);
			                tree.expandNode(tnode);
			            }
			        }
	           		grid.load({"parentId":orgId});
				 }else{
				 	grid.load();
				 }
				 
			 },
			 error : function() {
			 }
		  });
	}
	
	function csearch(){
		var orgNode = tree.getSelectedNode();
		var parentId = orgNode.id;//获取parentId
		
		grid.load({"parentId":parentId});
	   
	}
	
	/***查询***/
	function search() {
		var  searchKey= mini.get("searchKey").getValue();
		if(searchKey!=""){
			grid.load({"searchPhrase":searchKey});
		}else{
			grid.load();
		}
	}
	
	/******所属上级机构名称*****/
	function onOpenOrgName(e){
		var orgName="";
		var orgId = e.row.parentId;
		if(orgNameList!=null&&orgNameList.length>0){
			   for(var i=0;i<orgNameList.length;i++){
				   if(orgId==orgNameList[i].id){
					   orgName =  orgNameList[i].orgName;
					   break;
				   }  
			   }
		   }
		   return orgName;
	}
	
	var typeArray = getDict("org_type");
	/******所属组织属性类型*****/
	function onOpenOrgType(e){
		var name="";
		var value = e.row.orgType;
		if(typeArray!=null&&typeArray.length>0){
			   for(var i=0;i<typeArray.length;i++){
				   if(value==typeArray[i].id){
					   name =  typeArray[i].text;
					   break;
				   }  
			   }
		}
		return name;
	}
	
	var sevenArray = getDict("dq_7");
	function onOpenOrgSeven(e){
	
		var name="";
		var value = e.row.sevenId;
		
		//console.log(value+"---"+sevenArray);
		
		if(sevenArray!=null&&sevenArray.length>0){
			   for(var i=0;i<sevenArray.length;i++){
				   if(value==sevenArray[i].id){
					   name =  sevenArray[i].text;
					   break;
				   }  
			   }
		}
		return name;
	}
	
	
	
	/***获取组织机构名称  例如：全国/北京市/海淀区 ,flag true 删除斜杠  ***/
	function getAllOrgName(nodeArr,flag){
	
		var orgName = "";
		for (var i = nodeArr.length -1; i >=0; i--) {
	        orgName  = nodeArr[i].orgName+"/"+orgName; 
	    };
	    //是否删除斜杠
	    if(flag){
	    	orgName = orgName.substring(0,orgName.length -1);
	    }
		return orgName;
	}
	
	
	function getAllOrgId(nodeArr,flag){
	
		var orgId = "";
		for (var i = nodeArr.length -1; i >=0; i--) {
	        orgId  = nodeArr[i].id+","+orgId; 
	    };
	     //是否删除逗号
	    if(flag){
	    	orgId = orgId.substring(0,orgId.length -1);
	    }
		return '0,'+orgId;
	}
	
	
	
	var openU = [{ id: 1, text: '是' }, { id: 0, text: '否'}];
	function onOpenURenderer(e) {
	    for (var i = 0, l = openU.length; i < l; i++) {
	        var g = openU[i];
	        if (g.id == e.value) return g.text;
	    }
	    return "";
	}
	
	
	 function orgDimTreeSearch() {
	      var key = mini.get("key").getValue();
	      if (key == "") {
	          tree.clearFilter();
	      } else {
	          key = key.toLowerCase();                
	          tree.filter(function (node) {
	              var text = node.orgName ? node.orgName.toLowerCase() : "";
	              if (text.indexOf(key) != -1) {
	                  return true;
	              }
	          });
	      }
	  }
/*]]>*/
</script>
</html>